import datetime
import random

from django.contrib import admin
from django.db import models


class BaseModel(models.Model):
    create_time = models.DateTimeField('创建时间', auto_now=True)
    update_time = models.DateTimeField('更新时间', auto_now_add=True)

    class Meta:
        abstract = True


class User(BaseModel):
    SEX_SELECT = (
        (False, '男'),
        (True, '女')
    )
    user_code = models.IntegerField(auto_created=True)
    # 默认头像
    avatar = models.CharField(default="https://cacode-ssb.oss-cn-hangzhou.aliyuncs.com/avatars/default.png",
                              max_length=255, verbose_name="头像")
    name = models.CharField(max_length=255, verbose_name="类型的名字")
    password = models.CharField(max_length=32, verbose_name="密码,使用md5加密")
    phone = models.CharField(max_length=11, verbose_name="手机号")
    email = models.CharField(max_length=255, null=True, verbose_name="邮箱")
    sex = models.BooleanField(default=False, choices=SEX_SELECT, verbose_name="0男1女")
    birthday = models.DateTimeField(
        default=datetime.datetime.strptime('2001-01-01 00:00:00', '%Y-%m-%d %H:%M:%S'),
        verbose_name="默认生日为2001-01-01")
    invitation = models.ForeignKey('User', null=True, on_delete=models.CASCADE, verbose_name="邀请人")
    grade = models.IntegerField(default=1, verbose_name="等级")
    wxcode = models.CharField(max_length=255, null=True, verbose_name='微信用户的code', unique=True)

    class Meta:
        verbose_name = '用户表'
        db_table = 'baseData_user'
        verbose_name_plural = verbose_name


class ScoreDetails(BaseModel):
    STATUS_ADD_SUB = (
        (False, '减'),
        (True, '加')
    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="信誉分变化用户")
    status = models.BooleanField(default=False, choices=STATUS_ADD_SUB, verbose_name="1加0减")
    score = models.IntegerField(verbose_name='加减的分数')

    class Meta:
        db_table = 'baseData_scoredetails'


class Province(BaseModel):
    """省"""
    name = models.CharField(max_length=255, verbose_name="省的名字")

    class Meta:
        verbose_name = '省'
        db_table = 'baseData_province'
        verbose_name_plural = verbose_name


class City(BaseModel):
    """市"""
    city_index = models.IntegerField(verbose_name="城市的序号")
    province = models.ForeignKey(Province, on_delete=models.CASCADE, null=True, verbose_name="对应的省")
    name = models.CharField(max_length=255, verbose_name="城市的名字")

    class Meta:
        verbose_name = '市'
        db_table = 'baseData_city'
        verbose_name_plural = verbose_name


class District(BaseModel):
    """区"""
    name = models.CharField(max_length=255, verbose_name="地区的名字")
    city = models.ForeignKey(City, on_delete=models.CASCADE, null=True, verbose_name="对应的市")

    class Meta:
        verbose_name = '区'
        db_table = 'baseData_district'
        verbose_name_plural = verbose_name


admin.site.register([User, Province, City, District])
